<script setup>
import { ref } from 'vue'
import ChannelSelect from './ChannelSelect.vue'
import { Plus } from '@element-plus/icons-vue'
import { artAddService, artGetArtIdService, artSetArtService } from '@/api/article'
// import { baseURL, imageUrlToFile } from '@/utils/request'

// import { useUserStore } from '@/stores'
// import { storeToRefs } from 'pinia'
// const userStore = useUserStore()
// const { user } = storeToRefs(userStore)

const drawer = ref(false)
const title = ref('')
const imageUrl = ref('')
const forms = ref(null)
const formData = {
  Id: '',
  title: '',
  cate_id: '',
  content: '',
  cover_img: '',
  state: ''
}
const formModel = ref({
  ...formData
})
const emit = defineEmits(['success'])
const open = async (id) => {
  if (id) {
    title.value = '编辑文章'
    const { data } = await artGetArtIdService(id)
    formModel.value = { ...data }
    imageUrl.value = formModel.value.cover_img
    // http://127.0.0.1:3007\uploads\22d4c858eb9bffe04a7d6d460d00c114

    // 将图片 URL 转换为 File 对象
    // formModel.value.cover_img = await imageUrlToFile(
    //   imageUrl.value,
    //   formModel.value.cover_img
    // )
  } else {
    title.value = '添加文章'
  }
  drawer.value = true
}
const onUploadFile = (file) => {
  // imageUrl.value = URL.createObjectURL(file.raw)
  // formModel.value.cover_img = file.raw

  // 校验图片格式
  if (!['image/jpeg', 'image/png'].includes(file.raw.type)) {
    ElMessage.error('Avatar picture must be JPG or PNG format!')
    return
  }

  // 校验图片大小
  if (file.raw.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size cannot exceed 2MB!')
    return
  }

  const reader = new FileReader()
  reader.readAsDataURL(file.raw)
  reader.onload = () => {
    imageUrl.value = reader.result
    formModel.value.cover_img = reader.result
    // console.log(formModel.value.cover_img)
  }
}
const submit = async (state) => {
  formModel.value.state = state
  if (title.value === '编辑文章') {
    // 编辑
    await artSetArtService(formModel.value)
    ElMessage.success({ message: '编辑成功' })
  }
  if (title.value === '添加文章') {
    // 添加
    console.log(formModel.value)
    await artAddService(formModel.value)
    ElMessage.success({ message: '添加成功' })
  }
  drawer.value = false
  emit('success', formModel.value.id)
}
// 关闭清空数据
const close = () => {
  drawer.value = false
  formModel.value = formData
  imageUrl.value = ''
}
defineExpose({
  open
})
</script>
<template>
  <el-drawer :model-value="drawer" :title="title" direction="rtl" @close="close" size="50%">
    <!-- <el-avatar :src="user.user_pic || avatar" /> -->
    <!-- <img :src="user.user_pic" alt=""> -->
    <!-- 发表文章表单 -->
    <el-form :model="formModel" ref="forms" label-width="100px">
      <el-form-item label="文章标题" prop="title">
        <el-input v-model="formModel.title" placeholder="请输入标题"></el-input>
      </el-form-item>
      <ChannelSelect v-model="formModel.cate_id" width="100%"></ChannelSelect>

      <el-form-item label="文章封面" prop="cover_img">
        <el-upload
          class="avatar-uploader"
          :auto-upload="false"
          :show-file-list="false"
          :on-change="onUploadFile"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon">
            <Plus />
          </el-icon>
        </el-upload>
      </el-form-item>
      <el-form-item label="文章内容" prop="content">
        <el-input
          v-model="formModel.content"
          :rows="11"
          type="textarea"
          placeholder="请输入文章内容"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('已发布')">发布</el-button>
        <el-button type="info" @click="submit('草稿')">草稿</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>
<style scoped lang="less">
.avatar-uploader {
  border: 1px dashed var(--el-border-color);

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

  .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
  }

  .el-upload:hover {
    border-color: var(--el-color-primary);
  }

  .el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>
